/**
 * LICENSE AND TRADEMARK NOTICES
 * <p>
 * Except where noted, sample source code written by Motorola Mobility Inc. and
 * provided to you is licensed as described below.
 * <p>
 * Copyright (c) 2012, Motorola, Inc.
 * All  rights reserved except as otherwise explicitly indicated.
 * <p>
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * <p>
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * <p>
 * - Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * <p>
 * - Neither the name of Motorola, Inc. nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * <p>
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * <p>
 * Other source code displayed may be licensed under Apache License, Version
 * 2.
 * <p>
 * Copyright ¬© 2012, Android Open Source Project. All rights reserved unless
 * otherwise explicitly indicated.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0.
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

// Please refer to the accompanying article at 
// http://developer.motorola.com/docs/using_the_advanced_encryption_standard_in_android/

package com.taokatao.cardmee.api.security;

// A tutorial guide to using AES encryption in Android
// First we generate a 256 bit secret key; then we use that secret key to AES encrypt a plaintext message.
// Finally we decrypt the ciphertext to get our original message back.
// We don't keep a copy of the secret key - we generate the secret key whenever it is needed, 
// so we must remember all the parameters needed to generate it -
// the salt, the IV, the human-friendly passphrase, all the algorithms and parameters to those algorithms.
// Peter van der Linden, April 15 2012

import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class AES {
    private static final String UTF_8 = "UTF-8";
    private static final String CIPHER_MODE_PADDING = "AES/CBC/PKCS5Padding";
    private static final IvParameterSpec IV = new IvParameterSpec(new byte[]{0xE, 5, 0xD, 8, 7, 0xA, 3, 1, 0x17, 4, 2, 9, 12, 0xB, 0xF, 16});

    private SecretKeySpec skforAES;

    public AES(String secretKey) {
        skforAES = new SecretKeySpec(secretKey.getBytes(), "AES");
    }

    public final String encrypt(String value) {
        if (value == null) {
            return null;
        }

        try {
            Cipher c = Cipher.getInstance(CIPHER_MODE_PADDING);
            c.init(Cipher.ENCRYPT_MODE, skforAES, IV);
            byte[] encryptAfter = c.doFinal(value.getBytes(UTF_8));
            return URLEncoder.encode(Base64Encoder.encode(encryptAfter), UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public final String decrypt(String value) {
        if (value == null) {
            return null;
        }

        try {
            byte[] decryptBefore = Base64Decoder.decodeToBytes(URLDecoder.decode(value, UTF_8));
            Cipher c = Cipher.getInstance(CIPHER_MODE_PADDING);
            c.init(Cipher.DECRYPT_MODE, skforAES, IV);
            return new String(c.doFinal(decryptBefore), UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}